package com.bw.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.common.ResponseResult;
import com.bw.entity.ZkGoods;
import com.bw.entity.ZkOrder;
import com.bw.entity.ZkUser;
import com.bw.entity.res.ResOrder;
import com.bw.service.IZkGoodsService;
import com.bw.service.IZkOrderService;
import com.bw.service.IZkUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 莫晓晨
 * @since 2020-05-29
 */
@RestController
@RequestMapping("order")
@CrossOrigin
public class ZkOrderController {

    @Autowired
    private IZkOrderService orderService;

    @Autowired
    private IZkGoodsService goodsService;

    @Autowired
    private IZkUserService userService;

    @PostMapping("list")
    public ResponseResult list(@RequestBody ResOrder resOrder){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            QueryWrapper queryWrapper = new QueryWrapper();

            if(null!=resOrder.getGname()){
                queryWrapper.like("gname",resOrder.getGname());
            }

            List<ResOrder> list = orderService.resList(queryWrapper);
            result.setResult(list);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("defeat");
        }
        return result;
    }

    @PostMapping("save")
    public ResponseResult save(@RequestBody ZkOrder zkOrder){
        ResponseResult result = ResponseResult.SUCCESS();
        try{
            zkOrder.setStatus(0);
            zkOrder.setTime(new Date());
            zkOrder.setOid(0001);
            ZkUser user = userService.getById(zkOrder.getShopid());
            zkOrder.setCgman(user.getUname());
            orderService.saveOrUpdate(zkOrder);
        }catch (Exception e){

        }
        return result;
    }

    @PostMapping("shenhe/{id}")
    public ResponseResult shenhe(@PathVariable("id") Long id){
        ResponseResult result = ResponseResult.SUCCESS();
        try{
            ZkOrder order = orderService.getById(id);
            order.setStatus(1);
            order.setTime(new Date());
            orderService.saveOrUpdate(order);
            ZkGoods goods = goodsService.getById(order.getGid());
            goods.setCount(goods.getCount()+order.getNum());
            goodsService.saveOrUpdate(goods);
        }catch (Exception e){

        }
        return result;
    }

}
